package com.bkfm.framework.core.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.web.context.ContextLoader;
import org.yak.common.thread.ThreadPool;
import org.yak.core.exception.ServiceException;
import org.yak.core.nosql.MongoDBConfig;
import org.yak.core.nosql.entity.MongoSortField;
import org.yak.core.provider.SqlProvider;
import org.yak.core.utils.BeanUtils;

import com.bkfm.framework.core.api.service.IBkfmTableMetaServer;
import com.bkfm.framework.core.entity.BkfmFilter;
import com.bkfm.framework.core.entity.BkfmTableMetaData;
import com.bkfm.framework.core.service.BkfmTableMetaServer;

/**
 * 
 *业务模型缓存工具类
 * @平台：统一开发平台
 * @文件名称：BkfmTableMetaCacheUtils.java
 * @文件描述：
 * @author xuqiang
 * @日期：2018年7月19日上午8:31:10
 *
 */
public class BkfmTableMetaDataCacheUtils {

	private static final Logger logger = Logger.getLogger(BkfmTableMetaDataCacheUtils.class);
	
	/**
	 * 缓存数据库名称
	 */
	//private static final String MONGODB_NAME = "bkfm";
	
	/**
	 * 缓存数据业务模型表名
	 */
	//private static final String MONGODB_TABLEMETADATA_NAME = "blk_table_metadata";
	
	/*private static MongoDBConfig mongoConfig;
	
	static {
		String server_ip = ConfigUtils.getConfigValue("mongodb_server_ip");
		int server_port = Integer.parseInt(ConfigUtils.getConfigValue("mongodb_server_port"));
		mongoConfig = new MongoDBConfig(server_ip,server_port,MONGODB_NAME);
	}*/
	/**
	 * 初始化系统业务模型缓存
	 * @throws Exception
	 */
	public static void initBkfmTableMetaDataCache()throws Exception{
		IBkfmTableMetaServer bkfmModelService = ContextLoader.getCurrentWebApplicationContext().getBean(BkfmTableMetaServer.class);
		final List<BkfmTableMetaData> result = bkfmModelService.queryTableMetaAllList();
		MongoDBConfig.dropConnection(SqlProvider.MONGODB_NAME, SqlProvider.MONGODB_TABLEMETADATA_NAME);
		logger.info("开始启动系统服务任务...................Start!");
		ThreadPool.init().execute(new Runnable() {
			@Override
			public void run() {
				List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
				for(BkfmTableMetaData metaData : result) {
					Map<String,Object> metaMap = BeanUtils.trans2Map(metaData);
					/*if(metaMap.get("tablemetadata_id").equals("8DAC495580624B30BB44C2415A163C73")) {
						System.out.println(metaMap);
					}*/
					list.add(metaMap);
				}
				MongoDBConfig.insert(SqlProvider.MONGODB_NAME, SqlProvider.MONGODB_TABLEMETADATA_NAME, list);
				logger.info("开始启动系统服务完成...................End!");
			}
		});
	}
	
	/**
	 * 查询单挑业务模型数据
	 * @param bkfmFilter
	 * @return
	 * @throws Exception
	 */
	public static BkfmTableMetaData queryTableMetaColumn(BkfmFilter bkfmFilter)throws Exception{
		Map<String,Object> queryFilter = new HashMap<String,Object>();
		queryFilter.put("obj_path", bkfmFilter.getObj_path());
		List<BkfmTableMetaData> tableModelList = MongoDBConfig.queryEntityList(SqlProvider.MONGODB_NAME, SqlProvider.MONGODB_TABLEMETADATA_NAME, queryFilter, BkfmTableMetaData.class);
		if(tableModelList ==null || tableModelList.isEmpty()) {
			throw new ServiceException("获取缓存业务模型数据表数据异常:"+bkfmFilter.getObj_path());
		}
		BkfmTableMetaData tableModel = tableModelList.get(0);
		return tableModel;
	}
	
	/**
	 * 查询缓存业务模型表字段
	 * @param bkfmFilter
	 * @return
	 * @throws Exception
	 */
	public static List<BkfmTableMetaData> queryTableMetaColumnList(BkfmFilter bkfmFilter)throws Exception{
		BkfmTableMetaData tableModel = queryTableMetaColumn(bkfmFilter);
		Map<String,Object> queryFilter = new HashMap<String,Object>();
		queryFilter.put("father_tablemetadata_id", tableModel.getTablemetadata_id());
		
		List<MongoSortField> sortFieldList = new ArrayList<MongoSortField>();
		MongoSortField fi = new MongoSortField();
		fi.setOrder_by_field("order_by");
		fi.setOrder_by_type(MongoSortField.ASC);
		sortFieldList.add(fi);
		List<BkfmTableMetaData> fieldList = MongoDBConfig.queryEntityList(SqlProvider.MONGODB_NAME, SqlProvider.MONGODB_TABLEMETADATA_NAME, queryFilter, BkfmTableMetaData.class,sortFieldList);
		return fieldList;
	}
}
